package cn.openea.perpartner.meily.web.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Config;
import cn.openea.perpartner.meily.web.entity.dto.RemoveDto;
import cn.openea.perpartner.meily.web.service.IConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 参数配置表(MerryConfig)表控制层
 *
 * @author Weird077
 * @since 2022-12-01 18:22:38
 */
@Api(tags = "广告配置")
@RestController
@RequestMapping("/config")
public class ConfigController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private IConfigService configService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/page/list")
    public TableDataInfo pageList(@RequestBody Config config) {
        startPage();
        List<Config> list = configService.selectList(config);
        return getDataTable(list);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/list")
    public TableDataInfo selectAll(@RequestBody Config config) {
        List<Config> list = configService.selectList(config);
        return getDataTable(list);
    }

    @ApiOperation(value = "id查询")
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.configService.getById(id));
    }

    @ApiOperation(value = "key查询")
    @GetMapping("/get/{configKey}")
    public AjaxResult selectOne(@PathVariable String configKey) {
        Config config = configService.selectByKey(configKey);
        return BeanUtil.isNotEmpty(config)?success(config):error("未查询到数据");
    }

    @ApiOperation(value = "新增数据")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Config config) {
        if( config.getConfigId()== null){
            config.setCreateUser(UserContext.getUserId());
        }
        config.setUpdateUser(UserContext.getUserId());
        return toAjax(configService.insert(config));
    }
    @ApiOperation(value = "复制配置")
    @GetMapping("/copy/{configId}")
    public AjaxResult copy(@PathVariable String configId) {

        return toAjax(configService.copy(configId));
    }
    @ApiOperation(value = "删除数据")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody RemoveDto removeDto) {
        return toAjax(configService.delete(removeDto));
    }
}

